//
//  SCNStudyViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/4/25.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNStudyViewController: SCNTableViewController {

    var data: [SCNStudyListModels]?
      
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "学习"
        
        self.createTableView(view.frame, style: .plain, isRefreshNewData: false, isRefreshMoreData: false) { (table) in
            
            table.rowHeight = 60
            
            table.register(SCNToolsViewTableViewCell.classForCoder(), forCellReuseIdentifier: NSStringFromClass(SCNToolsViewTableViewCell.classForCoder()))
        }
        
        request()
    }
    
    override func request() {

        super.request()
        
        SCNStudyViewModel.requestOtherCate { (result) in
            self.data = result
            self.tableView.reloadData()
        }
    }
}

extension SCNStudyViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let tmp = data {
            return tmp.count
        }else {
            return 0
        }
        
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(SCNToolsViewTableViewCell.classForCoder()), for: indexPath) as! SCNToolsViewTableViewCell
        
        
        if let tmp = data {
            let obj = tmp[indexPath.row]
            
            cell.iconImageView.kf.setImage(with: URL(string:obj.logo_url!))
            cell.titleL.text = obj.title
            cell.detailL.text = obj.title_py
            
        }
        
        
        
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        defer {
            tableView.deselectRow(at: indexPath, animated: true)
        }
        
        if let tmp = data {
            if let tmpUrl = tmp[indexPath.row].url {
                self.loadWeb(at: self, url: tmpUrl, title: "")
            }


        }
        
    }
}
